package com.taotao.rop.manager;

import com.rop.security.AppSecretManager;
import com.taotao.rop.database.Constant;
import com.taotao.rop.database.domain.RopAppSecret;
import com.taotao.rop.database.mapper.RopAppSecretMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * ROP的密钥管理器
 */
public class RopAppSecretManager implements AppSecretManager {
    @Autowired
    private RopAppSecretMapper ropAppSecretMapper;

    @Override
    public String getSecret(String appKey) {
        RopAppSecret ropAppSecret = new RopAppSecret();
        ropAppSecret.setAppId(appKey);
        ropAppSecret.setStatus(Constant.STATUS_ENABLE);
        List<RopAppSecret> ropAppSecretList = ropAppSecretMapper.getList(ropAppSecret);
        if (ropAppSecretList == null || ropAppSecretList.size() <= 0) {
            return null;
        }
        return ropAppSecretList.get(0).getAppSecret();
    }

    @Override
    public boolean isValidAppKey(String appKey) {
        return getSecret(appKey) != null;
    }
}
